<template>
  <div id="andminclaimslist">
    <h1 id="h1">查看所有理赔申请</h1>
    <div>
      <el-table
        :data="
          claimsList.filter(
            (data) =>
              !search ||
              data.address.toLowerCase().includes(search.toLowerCase())
          )
        "
        style="width: 100%"
      >
        <el-table-column label="事故发生时间" prop="time"></el-table-column>
        <el-table-column label="事故发生地址" prop="address"> </el-table-column>
        <el-table-column label="申请理赔原因" prop="reason"> </el-table-column>
        <el-table-column label="理赔图集">
          <template slot-scope="scope">
            <el-button @click="lookClaimsImages(scope.row)"
              >查看理赔凭证</el-button
            >
          </template>
        </el-table-column>
        <el-table-column prop="state" label="理赔状态">
          <template slot-scope="scope">
            <el-tag type="warning" v-if="scope.row.state == 1">
              理赔申请中
            </el-tag>
            <el-tag type="success" v-else-if="scope.row.state == 2">
              理赔成功
            </el-tag>
            <el-tag type="danger" v-else-if="scope.row.state == 3">
              理赔失败
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div v-if="scope.row.state == 1">
              <el-button type="success" @click="myManageClaims(scope.row, 2)"
                >通过</el-button
              >
              <el-button type="danger" @click="myManageClaims(scope.row, 3)"
                >驳回</el-button
              >
            </div>
            <div v-else-if="scope.row.state == '2'">
              <el-tag type="success"> 已通过 </el-tag>
            </div>
            <div v-else-if="scope.row.state == '3'">
              <el-tag type="danger"> 已驳回 </el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="right">
          <template slot="header" slot-scope="scope">
            <el-input
              v-model="search"
              size="mini"
              placeholder="输入关键字搜索"
            />
          </template>
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="block" id="fenye">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage4"
          :page-sizes="[4, 5, 6]"
          :page-size="100"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <!-- 查看理赔图集 -->
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <!-- 渲染理赔图集 -->
      <div class="demo-image__lazy">
        <el-image
          v-for="(url, index) in urls"
          :key="index"
          :src="'http://localhost:3000/images/claimsimgs/' + url.imgUrl"
          lazy
        ></el-image>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapActions, mapState, mapMutations } = createNamespacedHelpers(
  "claims"
);
export default {
  created() {
    this.getClaims();
  },
  data() {
    return {
      search: "",
      updateModelShow: false,
      currentPage4: 1,
      urls: [],
      dialogVisible: false, //理赔图集弹框
    };
  },
  computed: {
    ...mapState(["claimsList", "total"]),
  },
  methods: {
    ...mapActions(["getClaims", "modifyClaims", "applyClaims", "deleteClaims"]),
    ...mapMutations(["getPageSize", "getCueerntPage"]),
    //处理理赔信息
    async myManageClaims(val, state) {
      // const data = await this.manageClaims({ _id: val._id, state });
      // if (data.status) {
      this.updateModelShow = false;

      const data = await this.applyClaims({ _id: val._id, state });
      if (data.data.status == "1") {
        this.getClaims();
        this.$message({
          message: "操作成功",
          type: "success",
        });
      } else {
        this.$message.error("操作失败");
      }
    },
    //修改理赔信息，状态为1 可以修改，状态为2 ，3 不可修改
    handleEdit(index, row) {
      this.centerDialogVisible = true;
      this.form = { ...row };
    },
    async handleDelete(index, row) {
      if (row.state == 2) {
      
        row.delete = !row.delete;
        const data = await this.deleteClaims({
          _id: row._id,
          delete: row.delete,
        });
        if (data.data.status == "1") {
          this.getClaims();
          this.$message({
            message: "删除成功",
            type: "success",
          });
        } else{
          this.$message.error('删除失败');
        }
      } else if(row.state == 1){
          this.$message({
          message: '请先处理理赔结果',
          type: 'warning'
        });
      }else if(row.state == 3){
        this.$message.error('理赔失败删除不了！请重新理赔');
      }
    },
    //每页几条数据
    handleSizeChange(val) {
      this.getPageSize(val);
      this.getClaims();
    },
    //当前页数
    handleCurrentChange(val) {
      this.getCueerntPage(val);
      this.getClaims();
    },
    //查看理赔图集
    lookClaimsImages(val) {
      this.dialogVisible = true;
      // console.log(758, val);
      this.urls = val.voucher;
      console.log(this.urls);
    },
    //关闭理赔图集弹框
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
  },
};
</script>
<style scopde lang='scss'>
#andminclaimslist {
  margin: 0;
  padding: 0;
  #h1 {
    border-bottom: 1px solid white;
    margin: 0;
    padding: 0;
    line-height: 50px;
    text-align: left;
  }
  #fenye {
    text-align: center;
    padding: 30px 0;
  }
}
</style>